/*
 * JuleView.java
 */
package jule;

import freemarker.core.Environment;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
// imports necesarios

// for filechooser
import javax.swing.JFileChooser;

// for clipboard operations


import javax.swing.tree.TreeSelectionModel;
import jule.clases.conf;
import jule.clases.field;
import jule.clases.util;
import jule.clases.project;
import jule.clases.table;
import jule.clases.treeFiles.FileNode;



import freemarker.template.*;
import java.awt.Color;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import jule.clases.fileUtil;
import jule.clases.editor.ftlEditorKit;
import jule.clases.field.typeField;
import jule.clases.treeFiles.databaseTreeRenderer;
import jule.clases.treeFiles.templateTreeRenderer;
import org.jdesktop.application.ResourceManager;
//import sun.security.pkcs11.wrapper.CK_ATTRIBUTE;


// for icon busy
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// for velocity
// requeriments
// commons-collections-3.2.1.jar
// commons-lang-2.4.jar

import java.io.StringWriter;
//import org.apache.log4j.Logger;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;


/**
 * The application's main frame.
 */
public class JuleView extends FrameView {

    private JFileChooser filechooser = new JFileChooser();
    private JFileChooser directoryChooser = new JFileChooser();
    private JFileChooser directoryChooser2 = new JFileChooser();
    private table tableSelected;
    private project projectSelected;
    private String templateSelected;
    private field fieldSelected;
    private ResourceManager res;
    private Component Icon;

    public JuleView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }

        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        // aqui ponemos el codigo para inicializar la aplicacion...

        util ut = new util();

        util.createUleUleDirectory();
        util.createTemplatesDirectory();
        util.createProjectTemplatesDirectory();
        util.createProjectsDirectory();
        util.createConfDirectory();



//        Icon icono = new ImageIcon(getClass().getResource("/images/page.png"));
//        jLabel1.setIcon(icono);

        jTree1.removeAll();
        jTree1.repaint();

        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        // para cambiarle el aspecto al tree
        jTree1.setCellRenderer(new databaseTreeRenderer());


        conf confi = new conf();
        try {
            confi = conf.load();
        } catch (Exception e) {
        }

        if (confi != null) {
            if (confi.getProyectoActual() != null) {

                projectSelected = confi.getProyectoActual();
                loadProject(projectSelected);
//                labProject.setText(confi.getProyectoActual().getName());
//                txtDirectory.setText(confi.getProyectoActual().getApplicationDirectory());
//                txtProjectTemplatesDirectory.setText(confi.getProjectTemplateDirectory());
//                fillTree(confi.getProyectoActual());
            } else {
                // falta poner en blanco el arbol...
            }
        }


        // cargamos el arbol de directorios...
        tFiles.removeAll();
        // tFiles.setExpandsSelectedPaths(true);
        tFiles.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        // para cambiarle el aspecto al tree
        tFiles.setCellRenderer(new templateTreeRenderer());
        //Set the icon for leaf nodes.
//        ImageIcon leafIcon = new ImageIcon(getClass().getResource("/images/table_link.png"));
//        if (leafIcon != null) {
//            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
//            renderer.setLeafIcon(leafIcon);
//            tFiles.setCellRenderer(renderer);
//        } else {
//            System.err.println("Leaf icon missing; using default.");
//        }


        tFiles.setModel(new jule.clases.treeFiles.FileSystemModel(ut.templatesDirectory));

        // le ponemos syntax higlihgt al editorpane....
        editor.setEditorKitForContentType("text/xml", new ftlEditorKit());
        editor.setContentType("text/xml");

    }

    public void loadProject(project pro) {

        jTree1.removeAll();
        jTree1.repaint();
        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        // para cambiarle el aspecto al tree
        jTree1.setCellRenderer(new databaseTreeRenderer());

        projectSelected = pro;
        labProject.setText(pro.getName());
//        txtDirectory.setText(pro.getApplicationDirectory());
//        txtProjectTemplatesDirectory.setText(pro.getProjectTemplatesDirectory());
        fillTree(pro);
    }

    @Action
    public void showNewProject() {

        project pro = new project();

        if (newproject == null) {
            JFrame mainFrame = JuleApp.getApplication().getMainFrame();
            editproject = new editProject(mainFrame, false);
            editproject.setLocationRelativeTo(mainFrame);
            editproject.setModal(true);
            editproject.setVisible(true);


            if (editproject.getNProject() != null) {
//                projectSelected = editproject.getNProject();
//                fillTree(projectSelected);

                projectSelected = editproject.getNProject();
                loadProject(projectSelected);
                fillTree(projectSelected);


                // lets save the config
                conf confi = new conf();
                confi.setProyectoActual(projectSelected);
                try {
                    conf.save(confi);
                } catch (Exception e) {
                }

            }
        }
//        JuleApp.getApplication().show(newproject);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = JuleApp.getApplication().getMainFrame();
            aboutBox = new JuleAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        JuleApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        labProject = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        labTableSelected = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        labTemplateSelected = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        txtNombreTabla = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTargetTableName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        txtNombreCampo = new javax.swing.JTextField();
        txtComment = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();
        cmbTargetType = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtTargetName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        ckIsKey = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        editor = new javax.swing.JEditorPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        tFiles = new javax.swing.JTree();
        butApplyTemplateFreemarker = new javax.swing.JButton();
        butCreateApplication = new javax.swing.JButton();
        butRefreshTemplates = new javax.swing.JButton();
        butEditTemplate = new javax.swing.JButton();
        butCopyToClipboard = new javax.swing.JButton();
        butCreateApplication1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtInfo = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        menuLoadProject = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenuItem();
        menuSaveProject = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        menuApplytemplate = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        menuCreateApplication = new javax.swing.JMenuItem();
        menuRefreshTemplates = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setMaximumSize(new java.awt.Dimension(876, 786));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(1180, 800));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setMaximumSize(new java.awt.Dimension(327, 327));
        jPanel2.setName("jPanel2"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jule.JuleApp.class).getContext().getResourceMap(JuleView.class);
        jLabel10.setIcon(resourceMap.getIcon("jLabel10.icon")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        labProject.setFont(resourceMap.getFont("labProject.font")); // NOI18N
        labProject.setText(resourceMap.getString("labProject.text")); // NOI18N
        labProject.setName("labProject"); // NOI18N

        jLabel11.setIcon(resourceMap.getIcon("jLabel11.icon")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        labTableSelected.setText(resourceMap.getString("labTableSelected.text")); // NOI18N
        labTableSelected.setName("labTableSelected"); // NOI18N

        jLabel12.setIcon(resourceMap.getIcon("jLabel12.icon")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        labTemplateSelected.setText(resourceMap.getString("labTemplateSelected.text")); // NOI18N
        labTemplateSelected.setName("labTemplateSelected"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTree1.setModel(null);
        jTree1.setName("jTree1"); // NOI18N
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jButton1.setIcon(resourceMap.getIcon("butRefreshDatabase.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("butRefreshDatabase.text")); // NOI18N
        jButton1.setName("butRefreshDatabase"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(labTemplateSelected, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labTableSelected, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labProject, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(labProject))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(labTableSelected))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labTemplateSelected)
                            .addComponent(jLabel12))
                        .addGap(6, 6, 6))
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel3.border.title"))); // NOI18N
        jPanel3.setMaximumSize(new java.awt.Dimension(327, 327));
        jPanel3.setName("jPanel3"); // NOI18N

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel7.border.title"))); // NOI18N
        jPanel7.setName("jPanel7"); // NOI18N

        txtNombreTabla.setText(resourceMap.getString("txtNombreTabla.text")); // NOI18N
        txtNombreTabla.setName("txtNombreTabla"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtTargetTableName.setName("txtTargetTableName"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jButton4.setToolTipText(resourceMap.getString("jButton4.toolTipText")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNombreTabla, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(txtTargetTableName, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(210, 210, 210)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNombreTabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtTargetTableName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
        );

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel8.border.title"))); // NOI18N
        jPanel8.setName("jPanel8"); // NOI18N

        txtNombreCampo.setText(resourceMap.getString("txtNombreCampo.text")); // NOI18N
        txtNombreCampo.setName("txtNombreCampo"); // NOI18N

        txtComment.setText(resourceMap.getString("txtComment.text")); // NOI18N
        txtComment.setName("txtComment"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        cmbType.setModel(new DefaultComboBoxModel(field.typeField.values()));
        cmbType.setName("cmbType"); // NOI18N

        cmbTargetType.setModel(new DefaultComboBoxModel(field.typeField.values()));
        cmbTargetType.setName("cmbTargetType"); // NOI18N

        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setToolTipText(resourceMap.getString("jButton3.toolTipText")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtTargetName.setName("txtTargetName"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        ckIsKey.setText(resourceMap.getString("ckIsKey.text")); // NOI18N
        ckIsKey.setName("ckIsKey"); // NOI18N

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbTargetType, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel8Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(ckIsKey)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel8Layout.createSequentialGroup()
                                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel8Layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(30, 30, 30)
                                        .addComponent(txtNombreCampo))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel8Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtTargetName, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtComment, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(46, 46, 46)))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombreCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtComment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtTargetName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(cmbTargetType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(ckIsKey))
                        .addContainerGap(22, Short.MAX_VALUE))
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addContainerGap())))
        );

        jLabel8.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabel8.AccessibleContext.accessibleName")); // NOI18N
        ckIsKey.getAccessibleContext().setAccessibleName(resourceMap.getString("jCheckBox1.AccessibleContext.accessibleName")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel4.border.title"))); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        editor.setContentType(resourceMap.getString("editor.contentType")); // NOI18N
        editor.setName("editor"); // NOI18N
        jScrollPane3.setViewportView(editor);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tFiles.setModel(null);
        tFiles.setName("tFiles"); // NOI18N
        tFiles.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                tFilesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(tFiles);

        butApplyTemplateFreemarker.setIcon(resourceMap.getIcon("butApplyTemplateFreemarker.icon")); // NOI18N
        butApplyTemplateFreemarker.setText(resourceMap.getString("butApplyTemplateFreemarker.text")); // NOI18N
        butApplyTemplateFreemarker.setToolTipText(resourceMap.getString("butApplyTemplateFreemarker.toolTipText")); // NOI18N
        butApplyTemplateFreemarker.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        butApplyTemplateFreemarker.setName("butApplyTemplateFreemarker"); // NOI18N
        butApplyTemplateFreemarker.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butApplyTemplateFreemarkerMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                butApplyTemplateFreemarkerMouseEntered(evt);
            }
        });

        butCreateApplication.setIcon(resourceMap.getIcon("butCreateApplication.icon")); // NOI18N
        butCreateApplication.setText(resourceMap.getString("butCreateApplication.text")); // NOI18N
        butCreateApplication.setToolTipText(resourceMap.getString("butCreateApplication.toolTipText")); // NOI18N
        butCreateApplication.setName("butCreateApplication"); // NOI18N
        butCreateApplication.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butCreateApplicationMouseClicked(evt);
            }
        });

        butRefreshTemplates.setIcon(resourceMap.getIcon("butRefreshTemplates.icon")); // NOI18N
        butRefreshTemplates.setText(resourceMap.getString("butRefreshTemplates.text")); // NOI18N
        butRefreshTemplates.setName("butRefreshTemplates"); // NOI18N
        butRefreshTemplates.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butRefreshTemplatesMouseClicked(evt);
            }
        });

        butEditTemplate.setIcon(resourceMap.getIcon("butEditTemplate.icon")); // NOI18N
        butEditTemplate.setText(resourceMap.getString("butEditTemplate.text")); // NOI18N
        butEditTemplate.setName("butEditTemplate"); // NOI18N
        butEditTemplate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butEditTemplateMouseClicked(evt);
            }
        });

        butCopyToClipboard.setIcon(resourceMap.getIcon("butCopyToClipboard.icon")); // NOI18N
        butCopyToClipboard.setText(resourceMap.getString("butCopyToClipboard.text")); // NOI18N
        butCopyToClipboard.setName("butCopyToClipboard"); // NOI18N
        butCopyToClipboard.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butCopyToClipboardMouseClicked(evt);
            }
        });

        butCreateApplication1.setIcon(resourceMap.getIcon("butCreateApplication1.icon")); // NOI18N
        butCreateApplication1.setToolTipText(resourceMap.getString("butCreateApplication1.toolTipText")); // NOI18N
        butCreateApplication1.setName("butCreateApplication1"); // NOI18N
        butCreateApplication1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butCreateApplication1MouseClicked(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 184, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 23, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(butApplyTemplateFreemarker, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(butCreateApplication, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(butCreateApplication1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(butRefreshTemplates))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(butEditTemplate, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butCopyToClipboard, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(butApplyTemplateFreemarker)
                    .addComponent(butCreateApplication)
                    .addComponent(butCreateApplication1)
                    .addComponent(butRefreshTemplates)
                    .addComponent(butEditTemplate)
                    .addComponent(butCopyToClipboard)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel6.setName("jPanel6"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        txtInfo.setColumns(20);
        txtInfo.setRows(5);
        txtInfo.setName("txtInfo"); // NOI18N
        jScrollPane4.setViewportView(txtInfo);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jule.JuleApp.class).getContext().getActionMap(JuleView.class, this);
        jMenuItem1.setAction(actionMap.get("showNewProject")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem1MouseClicked(evt);
            }
        });
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        menuLoadProject.setAction(actionMap.get("loadProject")); // NOI18N
        menuLoadProject.setText(resourceMap.getString("menuLoadProject.text")); // NOI18N
        menuLoadProject.setName("menuLoadProject"); // NOI18N
        menuLoadProject.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuLoadProjectMouseClicked(evt);
            }
        });
        menuLoadProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuLoadProjectActionPerformed(evt);
            }
        });
        fileMenu.add(menuLoadProject);

        menuEdit.setAction(actionMap.get("editSelectedProject")); // NOI18N
        menuEdit.setText(resourceMap.getString("menuEdit.text")); // NOI18N
        menuEdit.setName("menuEdit"); // NOI18N
        menuEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuEditMouseClicked(evt);
            }
        });
        menuEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditActionPerformed(evt);
            }
        });
        fileMenu.add(menuEdit);

        menuSaveProject.setAction(actionMap.get("saveProject")); // NOI18N
        menuSaveProject.setText(resourceMap.getString("menuSaveProject.text")); // NOI18N
        menuSaveProject.setName("menuSaveProject"); // NOI18N
        menuSaveProject.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuSaveProjectMouseClicked(evt);
            }
        });
        fileMenu.add(menuSaveProject);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        aboutMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aboutMenuItemMouseClicked(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        menuApplytemplate.setAction(actionMap.get("actionMenuApplyTemplate")); // NOI18N
        menuApplytemplate.setIcon(resourceMap.getIcon("menuApplytemplate.icon")); // NOI18N
        menuApplytemplate.setText(resourceMap.getString("menuApplytemplate.text")); // NOI18N
        menuApplytemplate.setName("menuApplytemplate"); // NOI18N
        menuApplytemplate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuApplytemplateActionPerformed(evt);
            }
        });
        jMenu1.add(menuApplytemplate);

        jMenuItem2.setAction(actionMap.get("actionMenuAppliesToAllTables")); // NOI18N
        jMenuItem2.setIcon(resourceMap.getIcon("menuAppliesToAllTables.icon")); // NOI18N
        jMenuItem2.setText(resourceMap.getString("menuAppliesToAllTables.text")); // NOI18N
        jMenuItem2.setName("menuAppliesToAllTables"); // NOI18N
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        menuCreateApplication.setAction(actionMap.get("actionMenuCreateApplication")); // NOI18N
        menuCreateApplication.setIcon(resourceMap.getIcon("menuCreateApplication.icon")); // NOI18N
        menuCreateApplication.setText(resourceMap.getString("menuCreateApplication.text")); // NOI18N
        menuCreateApplication.setName("menuCreateApplication"); // NOI18N
        menuCreateApplication.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCreateApplicationActionPerformed(evt);
            }
        });
        jMenu1.add(menuCreateApplication);

        menuRefreshTemplates.setAction(actionMap.get("actionMenuRefreshTemplates")); // NOI18N
        menuRefreshTemplates.setIcon(resourceMap.getIcon("menuRefreshTemplates.icon")); // NOI18N
        menuRefreshTemplates.setText(resourceMap.getString("menuRefreshTemplates.text")); // NOI18N
        menuRefreshTemplates.setName("menuRefreshTemplates"); // NOI18N
        jMenu1.add(menuRefreshTemplates);

        jMenuItem5.setText(resourceMap.getString("jMenuItem5.text")); // NOI18N
        jMenuItem5.setName("jMenuItem5"); // NOI18N
        jMenu1.add(jMenuItem5);

        jMenuItem6.setText(resourceMap.getString("jMenuItem6.text")); // NOI18N
        jMenuItem6.setName("jMenuItem6"); // NOI18N
        jMenu1.add(jMenuItem6);

        menuBar.add(jMenu1);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 859, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 1029, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void aboutMenuItemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aboutMenuItemMouseClicked
}//GEN-LAST:event_aboutMenuItemMouseClicked

private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MouseClicked
}//GEN-LAST:event_jMenuItem1MouseClicked

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void menuSaveProjectMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuSaveProjectMouseClicked
//proyecto.setNombre(nombre.getText())
}//GEN-LAST:event_menuSaveProjectMouseClicked

private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jTree1MouseClicked
// cuando cambiamos la selección del jtree
private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
// TODO add your handling code here:

// segun sea tabla o campo hacemos una cosa u otra...
    try {
        table tablita = (table) (jTree1.getLastSelectedPathComponent());
        tableSelected = tablita;
        labTableSelected.setText(tablita.getName());


        txtNombreTabla.setText(tablita.getName());
        txtTargetTableName.setText(tablita.getTargetName());
//                ImageIcon idleIcon =  createImageIcon("images/middle.gif", "The name of an image.");
//jLabel10.setIcon(idleIcon);

    } catch (Exception e) {
    }


    // we put the metadata of the field selected...
    try {
        field campo = (field) (jTree1.getLastSelectedPathComponent());
        fieldSelected = campo;

        txtNombreCampo.setText(campo.getNombre());
        txtComment.setText(campo.getComment());
        txtTargetName.setText(campo.getTargetName());
        ckIsKey.setSelected(campo.getisKey());

        cmbType.setSelectedItem(campo.getType());
        cmbTargetType.setSelectedItem(campo.getTargetType());

    } catch (Exception e) {
    }


}//GEN-LAST:event_jTree1ValueChanged

private void menuEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuEditMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_menuEditMouseClicked

// alguien ha seleccionado algo en archivos
private void tFilesValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_tFilesValueChanged
// TODO add your handling code here:
    try {
        FileNode archivo = (FileNode) (tFiles.getLastSelectedPathComponent());
        labTemplateSelected.setText(archivo.file.getName());
        // labTemplateSelectedFull.setText(archivo.file.getPath());
        templateSelected = archivo.file.getPath();


        // ahora vamos a rellenar el editor con el archivo
        FileReader fr = null;
        try {
            // load an pane text from a file...
            fr = new FileReader(templateSelected);
            editor.read(fr, null);
            fr.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (Exception e) {
    }

    try {
        field campo = (field) (jTree1.getLastSelectedPathComponent());

    } catch (Exception e) {
    }



}//GEN-LAST:event_tFilesValueChanged

private void butEditTemplateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butEditTemplateMouseClicked


    JFrame mainFrame = JuleApp.getApplication().getMainFrame();
    edittemplate = new editTemplate(mainFrame, false);
    edittemplate.setFileSelected(templateSelected);
    edittemplate.fillText();
    edittemplate.setLocationRelativeTo(mainFrame);
    edittemplate.setVisible(true);
}//GEN-LAST:event_butEditTemplateMouseClicked

private void butApplyTemplateFreemarkerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butApplyTemplateFreemarkerMouseClicked
// TODO add your handling code here:
    applyTemplate();
}//GEN-LAST:event_butApplyTemplateFreemarkerMouseClicked

    private void applyTemplate() {
// TODO add your handling code here:

        util ut = new util();
        freemarker.template.Template template;


        String templateSelectedx = new String("");
        String onlyNameTemplateSelected = new String("");
        String directoryTemplateSelected = new String("");
        templateSelectedx = templateSelected; //labTemplateSelectedFull.getText();


        if (templateSelectedx != null) {
             onlyNameTemplateSelected = fileUtil.filename(templateSelectedx);
            directoryTemplateSelected = fileUtil.dirname(templateSelectedx);
        }

        // now we look if there its a .vm file or a .ftl file
        if (templateSelected.indexOf(".vm") != -1) {
            try {
                String templateDirectory=null;
                templateDirectory = new File(directoryTemplateSelected).toString();
                Properties p = new Properties();
                p.setProperty("file.resource.loader.path", templateDirectory);
                Velocity.init( p );

                VelocityContext context = new VelocityContext();

                if (tableSelected == null) {
                    editor.setText("Please, select a table....");
                } else {

                    context.put("table", tableSelected);
                    context.put("project", projectSelected);

                    Template templateVelocity = null;
                    try {
                        templateVelocity = Velocity.getTemplate(onlyNameTemplateSelected);
                    } catch (ResourceNotFoundException rnfe) {
                        // couldn't find the template
                    } catch (ParseErrorException pee) {
                        // syntax error: problem parsing the template
                    }
                     catch (Exception e) {
                          appendError("Applied template to table: " + e.getMessage() + "\n");
                    }
                    StringWriter sw = new StringWriter();
                    templateVelocity.merge(context, sw);
                    editor.setText(sw.toString());

                }


            } catch (Exception ex) {
                Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
            }


        } else {
            StringWriter result = new StringWriter();
            Configuration cfg = new Configuration();
            if (tableSelected == null) {
                editor.setText("Please, select a table....");
            } else {
                try {

                    // para que no se coma los acentos en español...
                    cfg.setDefaultEncoding("UTF-8");

                    cfg.setDirectoryForTemplateLoading(new File(directoryTemplateSelected));
                    cfg.setObjectWrapper(new DefaultObjectWrapper());

                    /* Get or create a template */
                    freemarker.template.Template temp = cfg.getTemplate(onlyNameTemplateSelected);

                    /* Create a data-model */
                    Map root = new HashMap();
                    root.put("table", tableSelected);
                    root.put("project", projectSelected);



                    // si queremos sacar datos del template, que en este caso
                    // utilizamos para sacar el nombre del archivo a crear...
                    Environment env = temp.createProcessingEnvironment(root, result);
                    //env.setOutputEncoding("ISO-8859-1");
                    // si hacemos esta parte


                    env.process();  // process the template
                    TemplateModel nameFile = env.getVariable("nameFile");
                    TemplateModel extensionFile = env.getVariable("extensionFile");
                    TemplateModel languageGenerated = env.getVariable("languageGenerated");
                    TemplateModel description = env.getVariable("description");
                    TemplateModel targetDirectory = env.getVariable("targetDirectory");
                    TemplateModel appliesToAllTables = env.getVariable("targetDirectory");

                    result = new StringWriter();


                    // case the languageGenerated variable, the form
                    // of the parameters its different for the functions...
                    if (languageGenerated.toString().equals("php")) {
                        tableSelected.setListParameters(tableSelected.getListParametersForPhp());
                    }



                    temp.process(root, result);

                    editor.setText(result.toString());



                    appendError("Applied template to table: " + tableSelected.getName() + "\n");





                    //        /* Merge data-model with template */
//        Writer out = new OutputStreamWriter(System.out);
//        temp.process(root, out);
//        out.flush();


//        /* Merge data-model with template */
//        Writer out = new OutputStreamWriter(System.out);
//        temp.process(root, out);
//        out.flush();


                } catch (IOException ex) {
                    txtInfo.setText(ex.getMessage());
                    System.out.println("Message: " + ex.getMessage());

                    //Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TemplateException ex) {
                    txtInfo.setText(ex.getMessage());
                    System.out.println("Message: " + ex.getMessage());

                    // Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }






        }
    }

// function to put an error in the txtinfo jtext
//import javax.swing.text.AttributeSet;
//import javax.swing.text.Document;
//import javax.swing.text.SimpleAttributeSet;
//import javax.swing.text.StyleConstants;
    protected void appendError(String s) {
        // create some handy attribute sets
        //txtInfo.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

        Document d = txtInfo.getDocument();

        SimpleAttributeSet red = new SimpleAttributeSet();
        StyleConstants.setFontSize(red, 12);

        StyleConstants.setForeground(red, Color.red);
        StyleConstants.setBold(red, true);


        try {
            d.insertString(d.getLength(), s, red);
        } catch (BadLocationException ex) {
            Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

private void menuLoadProjectMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuLoadProjectMouseClicked
    // TODO add your handling code here:
}//GEN-LAST:event_menuLoadProjectMouseClicked

private void menuLoadProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuLoadProjectActionPerformed
    filechooser.setCurrentDirectory(new File(util.projectsDirectory));
    filechooser.setFileFilter(new jule.uleFilterFiles());
    filechooser.showOpenDialog(null);

    File file = filechooser.getSelectedFile();
    project pro = new project();
    try {
        pro = project.loadProject(file.getAbsolutePath());

        loadProject(pro);
//        labProject.setText(pro.getName());
//        txtDirectory.setText(pro.getApplicationDirectory());
//
//
//        projectSelected = pro;
//        fillTree(pro);

        // tambien lo guardamos en conf
        conf confi = new conf();
        confi.setProyectoActual(pro);

        conf.save(confi);

        confi = null;

    } catch (Exception ex) {
        Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
        // for debug

    }
}//GEN-LAST:event_menuLoadProjectActionPerformed

private void menuEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditActionPerformed

    if (projectSelected != null) {
        JFrame mainFrame = JuleApp.getApplication().getMainFrame();
        editProject ec = new editProject(mainFrame, false);
        ec.setModal(true);
        ec.setNProject(projectSelected);
        ec.setLocationRelativeTo(mainFrame);
        ec.setVisible(true);

        projectSelected = ec.getNProject();
        loadProject(projectSelected);
        fillTree(projectSelected);


        // lets save the config
        conf confi = new conf();
        confi.setProyectoActual(projectSelected);
        try {
            conf.save(confi);
        } catch (Exception e) {
        }


    }

}//GEN-LAST:event_menuEditActionPerformed
// applies the template to all the tables and save the ...

    private void ApplyTemplateToAllTables() {


        util ut = new util();
        Template template;

        String templateSelectedx = new String("");
        String nameTemplateSelected = new String("");
        String directoryTemplateSelected = new String("");
        templateSelectedx = templateSelected; //labTemplateSelectedFull.getText();

        if (templateSelected == null) {
            editor.setText("Please, select a template....");
        } else {


            for (table tablita : projectSelected.getTables()) {


                if (templateSelectedx != null) {
                    nameTemplateSelected = fileUtil.filename(templateSelectedx);
                    directoryTemplateSelected = fileUtil.dirname(templateSelectedx);
                }

                // we have to fix the tableSelected;

                StringWriter result = new StringWriter();
                StringWriter result2 = new StringWriter();
                Configuration cfg = new Configuration();

                try {

                    // para que no se coma los acentos en español...
                    cfg.setDefaultEncoding("UTF-8");
                    cfg.setDirectoryForTemplateLoading(new File(directoryTemplateSelected));
                    cfg.setObjectWrapper(new DefaultObjectWrapper());

                    /* Get or create a template */
                    freemarker.template.Template temp = cfg.getTemplate(nameTemplateSelected);



                    /* Create a data-model */
                    Map root = new HashMap();
                    root.put("table", tablita);
                    root.put("project", projectSelected);


                    // si queremos sacar datos del template, que en este caso
                    // utilizamos para sacar el nombre del archivo a crear...
                    Environment env = temp.createProcessingEnvironment(root, result);
                    env.process();  // process the template
                    TemplateModel nameFile = env.getVariable("nameFile");
                    TemplateModel extensionFile = env.getVariable("extensionFile");
                    TemplateModel languageGenerated = env.getVariable("languageGenerated");
                    TemplateModel description = env.getVariable("description");
                    TemplateModel targetDirectory = env.getVariable("targetDirectory");


                    temp.process(root, result2);
                    editor.setText(result2.toString());
                    txtInfo.append("Applied template to table: " + tablita.getName() + "\n");

                    String apDirectory = util.combinePaths(projectSelected.getApplicationDirectory(), targetDirectory.toString());
                    //projectSelected.getApplicationDirectory();


                    if (new File(apDirectory).exists()) {
                    } else {
                        boolean success = (new File(apDirectory)).mkdirs();
                    }

                    util.writeStringAsFile(apDirectory + nameFile + "." + extensionFile, result2.toString());
                    txtInfo.append("Saved file: " + apDirectory + nameFile + "." + extensionFile + "\n");

                } catch (IOException ex) {
                    txtInfo.append(ex.getMessage() + " for the table" + tablita.getName() + "\n");
                    System.out.println("Message: " + ex.getMessage());
                    //Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TemplateException ex) {
                    txtInfo.append(ex.getMessage());
                    System.out.println("Message: " + ex.getMessage() + " for the table" + tablita.getName() + "\n");
                    //Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }




    }

private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
}//GEN-LAST:event_fileMenuActionPerformed

private void butCreateApplicationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butCreateApplicationMouseClicked
    createApplication();
}//GEN-LAST:event_butCreateApplicationMouseClicked

    private void createApplication() {
        // TODO add your handling code here:
        try {
            mainPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            File targetDirectory = null;
            targetDirectory = new File(projectSelected.getApplicationDirectory());

            File initialDirectory = null;
            initialDirectory = new File(projectSelected.getProjectTemplatesDirectory());

            if (projectSelected.getApplicationDirectory() == null || projectSelected.getProjectTemplatesDirectory() == null) {
                txtInfo.append("Please see if application directory or project template directory its empty \n");


            } else {
                txtInfo.append("Target directory : " + targetDirectory.getName() + "\n");
                try {

                    if (targetDirectory.exists()) {
                    } else {
                        targetDirectory.mkdirs();
                        txtInfo.append("Created directory : " + targetDirectory.getName() + "\n");
                    }

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(Icon, "Error, try again", "Alert", JOptionPane.INFORMATION_MESSAGE);
                }
            }


            // traverseDirectoriesAndCopy(initialDirectory);
            traverse(initialDirectory);

            // JOptionPane.showMessageDialog(Icon, "Creation OK", "Alert", JOptionPane.INFORMATION_MESSAGE);
        } finally {
            mainPanel.setCursor(Cursor.getDefaultCursor());
        }



    }

    private void butRefreshTemplatesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butRefreshTemplatesMouseClicked
        refreshTemplates();
}//GEN-LAST:event_butRefreshTemplatesMouseClicked

    private void refreshTemplates() {
        util ut = new util();
        tFiles.setModel(new jule.clases.treeFiles.FileSystemModel(ut.templatesDirectory));
    }

private void butCopyToClipboardMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butCopyToClipboardMouseClicked
    // TODO add your handling code here:
    editor.selectAll();
    editor.copy();

}//GEN-LAST:event_butCopyToClipboardMouseClicked

private void butApplyTemplateFreemarkerMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butApplyTemplateFreemarkerMouseEntered
    // TODO add your handling code here:
}//GEN-LAST:event_butApplyTemplateFreemarkerMouseEntered

private void menuApplytemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuApplytemplateActionPerformed
}//GEN-LAST:event_menuApplytemplateActionPerformed

private void menuCreateApplicationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCreateApplicationActionPerformed
}//GEN-LAST:event_menuCreateApplicationActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
    try {
        fieldSelected.setNombre(txtNombreCampo.getText());
        fieldSelected.setTargetName(txtTargetName.getText());
        fieldSelected.setComment(txtComment.getText());
        fieldSelected.setisKey(ckIsKey.isSelected());

        fieldSelected.setTipo((typeField) cmbType.getSelectedItem());
        fieldSelected.setTargetType((typeField) cmbTargetType.getSelectedItem());

        projectSelected.saveProject(projectSelected);
        txtInfo.append(" \n Project saved : " + projectSelected.getName() + ".ule \n");

        // tambien lo guardamos en conf
        conf confi = new conf();
        confi.setProyectoActual(projectSelected);
        conf.save(confi);
        confi = null;



    } catch (Exception ex) {
        Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_jButton3MouseClicked

private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
    try {
        tableSelected.setName(txtNombreTabla.getText());
        tableSelected.setTargetName(txtTargetTableName.getText());


        projectSelected.saveProject(projectSelected);
        txtInfo.append(" \n Project saved : " + projectSelected.getName() + ".ule \n");

        // tambien lo guardamos en conf
        conf confi = new conf();
        confi.setProyectoActual(projectSelected);
        conf.save(confi);
        confi = null;



    } catch (Exception ex) {
        Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_jButton4MouseClicked

private void butCreateApplication1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butCreateApplication1MouseClicked


    util ut = new util();
    Template template;

    String templateSelectedx = new String("");
    String nameTemplateSelected = new String("");
    String directoryTemplateSelected = new String("");
    templateSelectedx = templateSelected; //labTemplateSelectedFull.getText();

    if (templateSelected == null) {
        editor.setText("Please, select a template....");
    } else {


        for (table tablita : projectSelected.getTables()) {


            if (templateSelectedx != null) {
                nameTemplateSelected = fileUtil.filename(templateSelectedx);
                directoryTemplateSelected = fileUtil.dirname(templateSelectedx);
            }

            // we have to fix the tableSelected;

            StringWriter result = new StringWriter();
            StringWriter result2 = new StringWriter();
            Configuration cfg = new Configuration();

            try {

                // para que no se coma los acentos en español...
                cfg.setDefaultEncoding("UTF-8");
                cfg.setDirectoryForTemplateLoading(new File(directoryTemplateSelected));
                cfg.setObjectWrapper(new DefaultObjectWrapper());

                /* Get or create a template */
                freemarker.template.Template temp = cfg.getTemplate(nameTemplateSelected);



                /* Create a data-model */
                Map root = new HashMap();
                root.put("table", tablita);
                root.put("project", projectSelected);


                // si queremos sacar datos del template, que en este caso
                // utilizamos para sacar el nombre del archivo a crear...
                Environment env = temp.createProcessingEnvironment(root, result);
                env.process();  // process the template
                TemplateModel nameFile = env.getVariable("nameFile");
                TemplateModel extensionFile = env.getVariable("extensionFile");
                TemplateModel languageGenerated = env.getVariable("languageGenerated");
                TemplateModel description = env.getVariable("description");
                TemplateModel targetDirectory = env.getVariable("targetDirectory");


                temp.process(root, result2);
                editor.setText(result2.toString());
                txtInfo.append("Applied template to table: " + tablita.getName() + "\n");

                String apDirectory = util.combinePaths(projectSelected.getApplicationDirectory(), targetDirectory.toString());
                //projectSelected.getApplicationDirectory();


                if (new File(apDirectory).exists()) {
                } else {
                    boolean success = (new File(apDirectory)).mkdirs();
                }

                util.writeStringAsFile(apDirectory + nameFile + "." + extensionFile, result2.toString());
                txtInfo.append("Saved file: " + apDirectory + nameFile + "." + extensionFile + "\n");

            } catch (IOException ex) {
                txtInfo.append(ex.getMessage() + " for the table" + tablita.getName() + "\n");
                System.out.println("Message: " + ex.getMessage());
                //Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TemplateException ex) {
                txtInfo.append(ex.getMessage());
                System.out.println("Message: " + ex.getMessage() + " for the table" + tablita.getName() + "\n");
                //Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
    }


}//GEN-LAST:event_butCreateApplication1MouseClicked

    private void traverse(File dir) {

        File targetDirectory = null;

        // we skip subversion directories...
        if (!dir.getAbsolutePath().contains("svn")) {

            if (dir.isDirectory()) {
                txtInfo.append("Found directory : " + dir.getName() + " -- ");

                //if not exists then....

                String finalDirectory = null;
                finalDirectory = dir.getAbsolutePath().replace(projectSelected.getProjectTemplatesDirectory(), "");
                targetDirectory = new File(util.combinePaths(projectSelected.getApplicationDirectory(), finalDirectory));

                if (targetDirectory.exists()) {
                } else {
                    targetDirectory.mkdirs();
                    Color colorActual = null;
                    colorActual = txtInfo.getForeground();
                    txtInfo.setForeground(new Color(33, 33, 33));
                    txtInfo.append("Created directory : " + targetDirectory.getPath() + "\n");
                    txtInfo.setForeground(colorActual);
                }


                String[] children = dir.list();
                for (int i = 0; i < children.length; i++) {
                    traverse(new File(dir, children[i]));
                }
            } else {

                // first, lets see if directory exists
                try {
                    targetDirectory = new File(dir.getAbsolutePath().substring(0, dir.getAbsolutePath().lastIndexOf("\\")));

                    if (targetDirectory.exists()) {
                    } else {
                        if (!targetDirectory.getAbsolutePath().contains("svn")) {
                            targetDirectory.mkdirs();
                            Color colorActual = null;
                            colorActual = txtInfo.getForeground();
                            txtInfo.setForeground(new Color(33, 33, 33));
                            txtInfo.append("Created directory : " + targetDirectory.getPath() + "\n");
                            txtInfo.setForeground(colorActual);
                        }
                    }
                } catch (Exception e) {

                    txtInfo.append("Error : " + e.getMessage() + "\n");
                }




                if (dir.getName().indexOf(".ftl") != -1) {
                    txtInfo.append("Found template : " + dir.getAbsolutePath() + "\n");
                    // directory where the template will be saved
                    String finalDirectory = "";
                    finalDirectory = dir.getAbsolutePath();
                    finalDirectory = finalDirectory.replace(projectSelected.getProjectTemplatesDirectory(), projectSelected.getApplicationDirectory());

                    applyTemplateToAllTables(dir.getAbsolutePath(), finalDirectory.replace(dir.getName(), ""));
                } else {
                    //not is a template, its a file...
                    txtInfo.append("Found file : " + dir.getName() + " -- ");

                    String finalFile = null;
                    finalFile = dir.getAbsolutePath().replace(projectSelected.getProjectTemplatesDirectory(), "");

                    targetDirectory = new File(util.combinePathAndFile(projectSelected.getApplicationDirectory(), finalFile));

                    try {
                        util.copyfile(dir.getAbsolutePath(), targetDirectory.getAbsolutePath());
                        // dir.renameTo(targetDirectory);
                        txtInfo.append("Copied file : " + targetDirectory.getName() + "\n");
                    } catch (Exception e) {

                        txtInfo.append("Error : " + e.getMessage() + "\n");
                    }

                }
            }

        }
    }

    // applies the template to all the tables in the projectSelected.getApplicationDirectory()
    public void applyTemplateToAllTables(String templateSelectedx) {
        util ut = new util();
        Template template;


        String nameTemplateSelected = new String("");
        String directoryTemplateSelected = new String("");
        templateSelected = templateSelectedx; //labTemplateSelectedFull.getText();


        for (table tablita : projectSelected.getTables()) {


            if (templateSelectedx != null) {
                nameTemplateSelected = fileUtil.filename(templateSelectedx);
                directoryTemplateSelected = fileUtil.dirname(templateSelectedx);
            }

            // we have to fix the tableSelected;

            StringWriter result = new StringWriter();
            StringWriter result2 = new StringWriter();
            Configuration cfg = new Configuration();

            try {

                // para que no se coma los acentos en español...
                cfg.setDefaultEncoding("UTF-8");
                cfg.setDirectoryForTemplateLoading(new File(directoryTemplateSelected));
                cfg.setObjectWrapper(new DefaultObjectWrapper());

                /* Get or create a template */
                freemarker.template.Template temp = cfg.getTemplate(nameTemplateSelected);



                /* Create a data-model */
                Map root = new HashMap();
                root.put("table", tablita);
                root.put("project", projectSelected);


                // si queremos sacar datos del template, que en este caso
                // utilizamos para sacar el nombre del archivo a crear...
                Environment env = temp.createProcessingEnvironment(root, result);
                env.process();  // process the template
                TemplateModel nameFile = env.getVariable("nameFile");
                TemplateModel extensionFile = env.getVariable("extensionFile");
                TemplateModel languageGenerated = env.getVariable("languageGenerated");
                TemplateModel description = env.getVariable("description");
                TemplateModel targetDirectory = env.getVariable("targetDirectory");


                temp.process(root, result2);
                editor.setText(result2.toString());


                String apDirectory = util.combinePaths(projectSelected.getApplicationDirectory(), targetDirectory.toString());
                //projectSelected.getApplicationDirectory();


                if (new File(apDirectory).exists()) {
                } else {
                    boolean success = (new File(apDirectory)).mkdirs();
                }

                util.writeStringAsFile(apDirectory + nameFile + "." + extensionFile, result2.toString());


            } catch (IOException ex) {
                editor.setText(ex.getMessage());
                System.out.println("Message: " + ex.getMessage());
                Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TemplateException ex) {
                editor.setText(ex.getMessage());
                System.out.println("Message: " + ex.getMessage());
                Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    // applies the template for all the tables , but with a directory...

    public void applyTemplateToAllTables(String templateSelectedx, String directory) {
        util ut = new util();
        freemarker.template.Template template;


        String nameTemplateSelected = new String("");
        String directoryTemplateSelected = new String("");
        templateSelected = templateSelectedx; //labTemplateSelectedFull.getText();


        for (table tablita : projectSelected.getTables()) {


            if (templateSelectedx != null) {
                nameTemplateSelected = fileUtil.filename(templateSelectedx);
                directoryTemplateSelected = fileUtil.dirname(templateSelectedx);
            }

            // we have to fix the tableSelected;

            StringWriter result = new StringWriter();
            StringWriter result2 = new StringWriter();
            Configuration cfg = new Configuration();

            try {

                // para que no se coma los acentos en español...
                cfg.setDefaultEncoding("UTF-8");
                cfg.setDirectoryForTemplateLoading(new File(directoryTemplateSelected));
                cfg.setObjectWrapper(new DefaultObjectWrapper());

                /* Get or create a template */
                freemarker.template.Template temp = cfg.getTemplate(nameTemplateSelected);



                /* Create a data-model */
                Map root = new HashMap();
                root.put("table", tablita);
                root.put("project", projectSelected);


                // si queremos sacar datos del template, que en este caso
                // utilizamos para sacar el nombre del archivo a crear...
                Environment env = temp.createProcessingEnvironment(root, result);
                env.process();  // process the template
                TemplateModel nameFile = env.getVariable("nameFile");
                TemplateModel extensionFile = env.getVariable("extensionFile");
                TemplateModel languageGenerated = env.getVariable("languageGenerated");
                TemplateModel description = env.getVariable("description");
                TemplateModel targetDirectory = env.getVariable("targetDirectory");
                TemplateModel appliesToAllTables = env.getVariable("appliesToAllTables");

                temp.process(root, result2);
                editor.setText(result2.toString());


                String apDirectory = util.combinePaths(directory, targetDirectory.toString());
                //projectSelected.getApplicationDirectory();


                if (new File(apDirectory).exists()) {
                } else {
                    boolean success = (new File(apDirectory)).mkdirs();
                }

                util.writeStringAsFile(apDirectory + nameFile + "." + extensionFile, result2.toString().replace("?//----", "//----"));

                //if the configuration property of the template appliesToAllTables its 
                // false then exit
                if (appliesToAllTables.equals("false")) {
                    // return();
                }

            } catch (IOException ex) {
                editor.setText(ex.getMessage());
                System.out.println("Message: " + ex.getMessage());
                Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);


            } catch (TemplateException ex) {
                editor.setText(ex.getMessage());
                System.out.println("Message: " + ex.getMessage());
                Logger.getLogger(JuleView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Action
    public void saveProject() {
        // vamos a grabar el proyecto

        try {
            project proyecto = new project();
            proyecto.saveProject(proyecto);
            proyecto =
                    null;
        } catch (Exception e) {
        }

    }

    private void fillTree(project pro) {

        jTree1.removeAll();
        jTree1.setExpandsSelectedPaths(true);
        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.setModel(new jule.clases.projectTreeModel(pro));
//        project prozz = (project) jTree1.getModel().getRoot();

    }

    @Action
    public void actionMenuApplyTemplate() {
        applyTemplate();
    }

    @Action
    public void actionMenuAppliesToAllTables() {
        ApplyTemplateToAllTables();
    }

    @Action
    public void actionMenuCreateApplication() {
        createApplication();
    }

    @Action
    public void actionMenuRefreshTemplates() {
        refreshTemplates();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butApplyTemplateFreemarker;
    private javax.swing.JButton butCopyToClipboard;
    private javax.swing.JButton butCreateApplication;
    private javax.swing.JButton butCreateApplication1;
    private javax.swing.JButton butEditTemplate;
    private javax.swing.JButton butRefreshTemplates;
    private javax.swing.JCheckBox ckIsKey;
    private javax.swing.JComboBox cmbTargetType;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JEditorPane editor;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTree jTree1;
    private javax.swing.JLabel labProject;
    private javax.swing.JLabel labTableSelected;
    private javax.swing.JLabel labTemplateSelected;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem menuApplytemplate;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuCreateApplication;
    private javax.swing.JMenuItem menuEdit;
    private javax.swing.JMenuItem menuLoadProject;
    private javax.swing.JMenuItem menuRefreshTemplates;
    private javax.swing.JMenuItem menuSaveProject;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTree tFiles;
    private javax.swing.JTextField txtComment;
    private javax.swing.JTextArea txtInfo;
    private javax.swing.JTextField txtNombreCampo;
    private javax.swing.JTextField txtNombreTabla;
    private javax.swing.JTextField txtTargetName;
    private javax.swing.JTextField txtTargetTableName;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private JFrame newproject;
    private editProject editproject;
    private editTemplate edittemplate;
}
